import java.util.Arrays;
import java.util.Scanner;

class Solution {
    //k次取反后的最大化的数组和
    public static int largestSumAfterKNegations(int[] nums, int k) {
        int m = 0;
        int sum = 0;
        //统计数组中负数的个数
        for(int i = 0; i < nums.length; i++){
            if(nums[i] < 0) m++;

            sum += nums[i];
        }
        Arrays.sort(nums);
        if(m >= k){//负数个数大于等于K
            for(int i = 0; i < k; i++){
                sum += 2*(-nums[i]);
            }
        }else{//负数个数小于k
            for(int i = 0; i < m; i++){
                nums[i] = -nums[i];
                sum += 2*nums[i];
            }
            if((k-m) % 2 == 1){
                Arrays.sort(nums);
                sum -= 2*nums[0];
            }
        }
        return sum;
    }

    //按身高排序
    public static String[] sortPeople(String[] names, int[] heights) {
        int len = names.length;
        Integer[] idx = new Integer[len];
        for(int i = 0; i < len; i++){
            idx[i] = i;
        }
        Arrays.sort(idx, (i, j) -> heights[j] - heights[i]);
        String[] ret = new String[len];
        for (int i = 0; i < len; i++) {
            ret[i] = names[idx[i]];
        }
        return ret;
    }

    //优势洗牌
    public int[] advantageCount(int[] nums1, int[] nums2) {
        int n = nums2.length;
        Integer[] idx2 = new Integer[n];
        for (int i = 0; i < n; ++i) {
            idx2[i] = i;
        }
        Arrays.sort(idx2, (i, j) -> nums2[i] - nums2[j]);
        Arrays.sort(nums1);
        int[] ret = new int[n];
        int left = 0, right = n - 1;
        for (int i = 0; i < n; ++i) {
            if(nums1[i] > nums2[idx2[left]]){
                ret[idx2[left]] = nums1[i];
                left++;
            }else{
                ret[idx2[right]] = nums1[i];
                right--;
            }
        }
        return ret;
    }
    //最长回文串
    public int longestPalindrome(String s) {
        int[] hash = new int[60];
        for(int i = 0; i < s.length(); i++){
            int index = s.charAt(i) - 'A';
            hash[index]++;
        }
        int ret = 0;
        for(int i = 0; i < 60; i++){
            ret += hash[i] / 2 * 2;
        }
        if(ret < s.length()) ret += 1;
        return ret;
    }
    //增减字符串匹配
    public int[] diStringMatch(String s) {
        int len = s.length();
        int[] ret = new int[len + 1];
        int left = 0, right = len;
        for(int i = 0; i < len; i++){
            if(s.charAt(i) == 'I'){
                ret[i] = left++;
            }else{
                ret[i] = right--;
            }
        }
        ret[len] = left;
        return ret;
    }

    //查找两个字符串a,b中的最长公共子串
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s1 = in.nextLine();
            String s2 = in.nextLine();
            int m = s1.length();
            int n = s2.length();
            if(m > n){
                String tmp = s1;
                s1 = s2;
                s2 = tmp;
                int t = m;
                m = n;
                n = t;
            }
            int[][] dp = new int[m+1][n+1];
            int max = 0;
            int end = 0;
            for(int i = 1; i <= m; i++){
                for(int j = 1; j <= n; j++){
                    if(s1.charAt(i-1) == s2.charAt(j-1)){
                        dp[i][j] = dp[i-1][j-1] + 1;
                        if(dp[i][j] > max){
                            max = dp[i][j];
                            end = i;
                        }
                    }else{
                        dp[i][j] = 0;
                    }
                }
            }
            String ret = s1.substring(end-max, end);
            System.out.println(ret);
        }
    }
    //公共子串计算
    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        String s1 = in.nextLine();
        String s2 = in.nextLine();
        int m = s1.length();
        int n = s2.length();
        int[][] dp = new int[m+1][n+1];
        int max = 0;
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(s1.charAt(i-1) == s2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1] + 1;
                    if(dp[i][j] > max) max = dp[i][j];
                }
            }
        }
        System.out.println(max);
    }
    public static void main(String[] args) {
        int[] nums = {3,-1,0,2};
        String[] name = {"Mary","John","Emma"};
        int[] heights = {180,165,170};
       // largestSumAfterKNegations(nums, 3);
        sortPeople(name, heights);
    }
}